package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"log"
	"mishop/entitys"
	"mishop/models"
	"mishop/services"
	"mishop/utils"
	"strconv"
)

type IOrderItem interface {
	All(ctx *gin.Context)
	Show(ctx *gin.Context)
	Store(ctx *gin.Context)
	Update(ctx *gin.Context)
	Delete(ctx *gin.Context)
}
type OrderItem struct {
	service services.IOrderItemService
}

func (c *OrderItem) All(ctx *gin.Context) {
	pageEntity := entitys.PageEntity{}
	err := ctx.ShouldBindWith(&pageEntity, binding.JSON)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}

	userID := 0
	user_id := ctx.Query("user_id")
	if user_id != "" {
		parseInt, err := strconv.Atoi(user_id)
		utils.HandleErrors(err, ctx, 400, 1, "")
		if err != nil {
			return
		}
		userID = parseInt
	} else {
		utils.HandleErrors(err, ctx, 400, 1, "参数不正确")
		return
	}
	pages, err := c.service.FindByUserID(pageEntity.Page, pageEntity.Rows, uint(userID))
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	utils.HandleSucess(ctx, 200, 0, "", pages)
}

func (c *OrderItem) Show(ctx *gin.Context) {
	address_id := ctx.Param("address_id")
	addressID, err := strconv.Atoi(address_id)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}

	address, err := c.service.FindByID(uint(addressID))
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}

	utils.HandleSucess(ctx, 200, 0, "", address)
}

func (c *OrderItem) Store(ctx *gin.Context) {

	orderEntity := models.OrderItem{}
	err := ctx.ShouldBindWith(&orderEntity, binding.JSON)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	order, err := c.service.Create(&orderEntity)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	utils.HandleSucess(ctx, 201, 0, "", order)

}

func (c *OrderItem) Update(ctx *gin.Context) {
	order_id := ctx.Param("order_id")
	orderID, err := strconv.Atoi(order_id)
	utils.HandleErrors(err, ctx, 400, 1, "")
	log.Print(orderID)
	order_item_id := ctx.Param("order_item_id")
	orderItemID, err := strconv.Atoi(order_item_id)
	utils.HandleErrors(err, ctx, 400, 1, "")

	orderItemEntity := models.OrderItem{}
	err = ctx.ShouldBindWith(&orderItemEntity, binding.JSON)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	orderItemEntity.ID = orderItemID

	orderItem, err := c.service.Update(&orderItemEntity)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	utils.HandleSucess(ctx, 201, 0, "", orderItem)
}

func (c *OrderItem) Delete(ctx *gin.Context) {
	address_id := ctx.Param("address_id")
	addressID, err := strconv.Atoi(address_id)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	err = c.service.Delete(addressID)
	utils.HandleErrors(err, ctx, 400, 1, "")
	if err != nil {
		return
	}
	utils.HandleSucess(ctx, 200, 0, "删除成功", nil)

}

func NewOrderItem() IOrderItem {
	service := services.NewOrderItemService(
		models.NewOrderItem(models.Db),
		models.NewOrder(models.Db),
		models.NewProduct(models.Db),
	)
	return &OrderItem{service: service}
}
